{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['21082701.pdf', '21082702.pdf', '21082703.pdf', '21082704.pdf', 'combined.pdf', 'pro230418_1.pdf', 'pro230418_2.pdf', 'pro230418_3.pdf']\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "合并pdf，剔除首页\n",
    "'''\n",
    "# 使用endswith过滤pdf\n",
    "\n",
    "# 这写代码在jupyter执行没问题，但是新建py执行不了\n",
    "# python的执行目录不是.py所在目录，里面没有pdf文件，所以生成的文件打不开\n",
    "# %%writefile a1.py\n",
    "# 这行不能在vscode中执行\n",
    "\n",
    "import PyPDF2 as pp\n",
    "import os\n",
    "\n",
    "# 列出文件夹内所有pdf并排序\n",
    "a_files = []\n",
    "for f in os.listdir('.'):\n",
    "    if f.endswith('.pdf'):\n",
    "        a_files.append(f)\n",
    "a_files.sort(key=str.lower) #将文件按字母顺序排序\n",
    "print(a_files)\n",
    "\n",
    "# 将所有pdf页面写入pdf_wr\n",
    "pdf_wr = pp.PdfWriter()\n",
    "# print(pdf_wr)\n",
    "# print(pdf_files)\n",
    "for f2 in a_files:\n",
    "    pdf_obj = open(f2, 'rb')\n",
    "    pdf_r = pp.PdfReader(pdf_obj)\n",
    "    for page_num in range(1, len(pdf_r.pages)): #剔除首页封面，一般使用，不用剔除\n",
    "        page_obj = pdf_r.pages[page_num]\n",
    "        pdf_wr.add_page(page_obj)\n",
    "\n",
    "# 将pdf_wr写入文件\n",
    "pdf_out = open('combined.pdf', 'wb')\n",
    "pdf_wr.write(pdf_out)\n",
    "pdf_out.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['051601279 #主被动立柱外壳# B.pdf', '051601280 #主被动立柱外壳# B.pdf', '051601281 #主被动底座外壳# B.pdf', '051601282 #主被动底座外壳# B.pdf']\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "合并pdf，不剔除首页\n",
    "'''\n",
    "\n",
    "\n",
    "\n",
    "import PyPDF2 as pp\n",
    "import os\n",
    "\n",
    "# 列出文件夹内所有pdf并排序\n",
    "a_files = []\n",
    "for f in os.listdir('.'):\n",
    "    if f.endswith('.pdf'):\n",
    "        a_files.append(f)\n",
    "a_files.sort(key=str.lower) #将文件按字母顺序排序\n",
    "print(a_files)\n",
    "\n",
    "# 将所有pdf页面写入pdf_wr\n",
    "pdf_wr = pp.PdfWriter()\n",
    "# print(pdf_wr)\n",
    "# print(pdf_files)\n",
    "for f2 in a_files:\n",
    "    pdf_obj = open(f2, 'rb')\n",
    "    pdf_r = pp.PdfReader(pdf_obj)\n",
    "    for page_num in range(len(pdf_r.pages)): \n",
    "        page_obj = pdf_r.pages[page_num]\n",
    "        pdf_wr.add_page(page_obj)  #合并时只能按页，能否将整个文档添加？\n",
    "\n",
    "# 将pdf_wr写入文件\n",
    "pdf_out = open('combined.pdf', 'wb')\n",
    "pdf_wr.write(pdf_out)\n",
    "pdf_out.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['pro230418_1.pdf', 'pro230418_2.pdf', 'pro230418_3.pdf']\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "合并pro开头的pdf\n",
    "'''\n",
    "# 使用正则过滤pdf，更具一般性\n",
    "\n",
    "import PyPDF2 as pp\n",
    "import os, re\n",
    "\n",
    "# 列出文件夹内所有pdf并排序\n",
    "a_files = []\n",
    "for f in os.listdir('.'):\n",
    "    if re.match(r'pro.*?.pdf', f):\n",
    "        a_files.append(f)\n",
    "a_files.sort(key=str.lower) #将文件按字母顺序排序\n",
    "print(a_files)\n",
    "\n",
    "# 将所有pdf页面写入pdf_wr\n",
    "pdf_wr = pp.PdfWriter()\n",
    "# print(pdf_wr)\n",
    "# print(pdf_files)\n",
    "for f2 in a_files:\n",
    "    pdf_obj = open(f2, 'rb')\n",
    "    pdf_r = pp.PdfReader(pdf_obj)\n",
    "    for page_num in range(len(pdf_r.pages)): \n",
    "        page_obj = pdf_r.pages[page_num]\n",
    "        pdf_wr.add_page(page_obj)  #合并时只能按页，能否将整个文档添加？\n",
    "\n",
    "# 将pdf_wr写入文件\n",
    "pdf_out = open('pro.pdf', 'wb')\n",
    "pdf_wr.write(pdf_out)\n",
    "pdf_out.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['23112202_1.pdf', '23112202_2.pdf']\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "合并文件名结尾带_1, _2的pdf\n",
    "'''\n",
    "\n",
    "# 只会用正则将所有带数字后缀合并\n",
    "\n",
    "\n",
    "\n",
    "import PyPDF2 as pp\n",
    "import os, re\n",
    "\n",
    "\n",
    "path = 'Z:/virtual_share/合并pdf'\n",
    "\n",
    "# path = 'D:/主被动归档/主被动1.5_图纸归档_3'\n",
    "\n",
    "# path = 'C:/Users/xiaobojie/Desktop/图纸外发/主被动1.5代 修改底座上肢架'\n",
    "\n",
    "os.chdir(path)\n",
    "\n",
    "\n",
    "# 列出文件夹内所有pdf并排序\n",
    "a_files = []\n",
    "for f in os.listdir('.'):\n",
    "    if re.match(r'.*?_\\d.pdf', f):\n",
    "        a_files.append(f)\n",
    "a_files.sort(key=str.lower) #将文件按字母顺序排序\n",
    "print(a_files)\n",
    "\n",
    "# 将所有pdf页面写入pdf_wr\n",
    "pdf_wr = pp.PdfWriter()\n",
    "# print(pdf_wr)\n",
    "# print(pdf_files)\n",
    "for f2 in a_files:\n",
    "    pdf_obj = open(f2, 'rb')\n",
    "    pdf_r = pp.PdfReader(pdf_obj)\n",
    "    for page_num in range(len(pdf_r.pages)): \n",
    "        page_obj = pdf_r.pages[page_num]\n",
    "        pdf_wr.add_page(page_obj)  #合并时只能按页，能否将整个文档添加？\n",
    "    pdf_obj.close()\n",
    "\n",
    "# 将pdf_wr写入文件\n",
    "pdf_out = open('pro.pdf', 'wb')\n",
    "pdf_wr.write(pdf_out)\n",
    "pdf_out.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如何用正则读取前半部分相同的文件，例20061003_1.pdf，20061003_2.pdf？\n",
    "\n",
    "\n",
    "import PyPDF2 as pp\n",
    "import os, re\n",
    "\n",
    "path = 'D:/主被动归档/test_for_code'\n",
    "os.chdir(path)\n",
    "# print(os.getcwd())\n",
    "\n",
    "\n",
    "# 列出文件夹内所有pdf并排序\n",
    "a_files = []\n",
    "for f in os.listdir('.'):\n",
    "    if re.match(r'.*pdf', f):\n",
    "        a_files.append(f)\n",
    "a_files.sort(key=str.lower) #将文件按字母顺序排序\n",
    "# print(pdf_files)\n",
    "\n",
    "# 将所有pdf页面写入pdf_wr\n",
    "pdf_wr = pp.PdfWriter()\n",
    "# print(pdf_wr)\n",
    "# print(pdf_files)\n",
    "for f2 in a_files:\n",
    "    pdf_obj = open(f2, 'rb')\n",
    "    pdf_r = pp.PdfReader(pdf_obj)\n",
    "    for page_num in range(len(pdf_r.pages)): \n",
    "        page_obj = pdf_r.pages[page_num]\n",
    "        pdf_wr.add_page(page_obj)  #合并时只能按页，能否将整个文档添加？\n",
    "\n",
    "# 将pdf_wr写入文件\n",
    "pdf_out = open('pro.pdf', 'wb')\n",
    "pdf_wr.write(pdf_out)\n",
    "pdf_out.close()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  },
  "vscode": {
   "interpreter": {
    "hash": "3c06e3e46abf38078fe4dac36a0085ec2b134ebbd73dd076183d243eeca6918f"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
